package cn.gz.lbs.servlet;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.gz.lbs.main.SPRequestManager;
import cn.gz.zoomtech.common.ConfManager;
import cn.gz.zoomtech.common.db.DbHandle;


import test.Test;

/**
 * Servlet implementation class LbsProxyServer
 */
public class LbsProxyServer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private static final Log log = LogFactory.getLog(LbsProxyServer.class);
	 private static CompositeConfiguration cf = ConfManager.getInstance().getConf();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LbsProxyServer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		
//		log.info("...DB testing start...");
//		DbHandle dbh = new DbHandle();
//		String msisdn = "13760654681";
//		dbh.checkIsWhite(msisdn);
//		String local_url =  cf.getString("local_url", "local_url");
//		
//		log.info("...DB testing end...local_url:"+local_url);
		
//		  step0: 客户端IP信息：
		  String clientIp = request.getRemoteHost();
//		  log.debug("clientIp:"+clientIp);
//        step1: 读取客户端请求,输出请求字符串
	      InputStreamReader reader = new InputStreamReader(request.getInputStream(), "utf-8"); 
	      char[] buff = new char[1024]; 
	      int length = 0; 
	      String reqStrs = "";
	      while ((length = reader.read(buff)) != -1) {
	    	  reqStrs = reqStrs + new String(buff, 0, length);
	      }
	      log.debug("clientReqStr0:"+reqStrs);
	      
	      reqStrs = reqStrs.replace("\r","");
	      log.info("r_clientReqStr1:"+reqStrs);
	      
	      reqStrs = reqStrs.replace("\n","");
	      log.debug("n_clientReqStr2:"+reqStrs);
	      
	      reqStrs = reqStrs.replace("\t","");
	      log.debug("t_clientReqStr3:"+reqStrs);
	      
	      reqStrs = reqStrs.replace("        ","");
	      log.debug("        _clientReqStr4:"+reqStrs);
	      
	      
	      
		SPRequestManager sprM = new SPRequestManager();
		String respStr = sprM.dealwithSpReq(reqStrs,clientIp);
//		String respStr = Test.testLia();
//      step2: 返回响应给客户端
		response.setContentType("text/xml"); 
		response.setCharacterEncoding("utf-8"); 
        PrintWriter out = response.getWriter();
        out.println(respStr); 	
//		log.info("deal post OK...........respStr:"+respStr);
	}

}
